using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The DayGroupCodeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about DayGroupCodeTable objects.
    /// </summary>
    public class DayGroupCodeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of DayGroupCodeTable from the underlying datasource.
        /// </summary>
        /// <param name="dayGroupCode">The unique DayGroupCode of the DayGroupCodeTable in the database.</param>
        /// <returns>An DayGroupCodeTable when the DayGroupCode was found in the database, or null otherwise.</returns>
        public static DayGroupCodeTable GetItem(short dayGroupCode)
        {
            DayGroupCodeTable myDayGroupCodeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDayGroupCodeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@dayGroupCode", dayGroupCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myDayGroupCodeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myDayGroupCodeTable;
            }
        }

        /// <summary>
        /// Returns a list with DayGroupCodeTable objects.
        /// </summary>
        /// <returns>A generics List with the DayGroupCodeTable objects.</returns>
        public static DayGroupCodeTableList GetList()
        {
            DayGroupCodeTableList tempList = new DayGroupCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDayGroupCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a DayGroupCodeTable in the database.
        /// </summary>
        /// <param name="myDayGroupCodeTable">The DayGroupCodeTable instance to save.</param>
        /// <returns>The new DayGroupCode if the DayGroupCodeTable is new in the database or the existing DayGroupCode when an item was updated.</returns>
        public static Int16 Save(DayGroupCodeTable myDayGroupCodeTable)
        {
            Int16 result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDayGroupCodeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myDayGroupCodeTable.DayGroupCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@dayGroupCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@dayGroupCode", myDayGroupCodeTable.DayGroupCode);
                    }
                    if (string.IsNullOrEmpty(myDayGroupCodeTable.DayDescriptionList))
                    {
                        myCommand.Parameters.AddWithValue("@dayDescriptionList", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@dayDescriptionList", myDayGroupCodeTable.DayDescriptionList);
                    }
                    if (string.IsNullOrEmpty(myDayGroupCodeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myDayGroupCodeTable.Description);
                    }
                    if (myDayGroupCodeTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myDayGroupCodeTable.Status);
                    }
                    if (myDayGroupCodeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myDayGroupCodeTable.CreatedDateTime);
                    }
                    if (myDayGroupCodeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myDayGroupCodeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a DayGroupCodeTable from the database.
        /// </summary>
        /// <param name="dayGroupCode">The DayGroupCode of the DayGroupCodeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short dayGroupCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocDayGroupCodeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@dayGroupCode", dayGroupCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the DayGroupCodeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static DayGroupCodeTable FillDataRecord(IDataRecord myDataRecord)
        {
            DayGroupCodeTable myDayGroupCodeTable = new DayGroupCodeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DayGroupCode")))
            {
                myDayGroupCodeTable.DayGroupCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DayGroupCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DayDescriptionList")))
            {
                myDayGroupCodeTable.DayDescriptionList = myDataRecord.GetString(myDataRecord.GetOrdinal("DayDescriptionList"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myDayGroupCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myDayGroupCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myDayGroupCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myDayGroupCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myDayGroupCodeTable;
        }
    }

}
